﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace TaxiDispatchClient
{
    public partial class AdMachineAddForm : DevExpress.XtraEditors.XtraForm
    {
        public AdMachineAddForm()
        {
            InitializeComponent();
          
        }

        //添加
        public AdMachineAddForm(double lat,double lon)
        {
            InitializeComponent();
            textEdit_latitude.Text = lat.ToString("f5");
            textEdit_longitude.Text = lon.ToString("f5");
            simpleButton_add.Visible = true;
            simpleButton_cancel.Visible = true;
            Service.ExcelCommon.LoadDataFromExcel(Environment.CurrentDirectory + "\\temp.xls");
        }
        // 查看、编辑
        public AdMachineAddForm(ADMachineInfo model)
        {
            InitializeComponent();
            textBox_number.Text = model.Number;
            textEdit_stationName.Text = model.StationName  ;
            textEdit_longitude.Text = model.Longitude.ToString();
            textEdit_latitude.Text =model.Latitude.ToString();
            textEdit_linkman.Text = model.Linkman;
            textEdit_telphone.Text = model.Telephone;
            textEdit_phone.Text =model.PhoneNumber;
            textEdit_address.Text = model.Address;
            textEdit_internetType.Text =model.InternetType;
            textEdit_remoteId.Text =model.XTRemoteId;
            textEdit_exitPwd.Text =model.ExitPassword;
            textEdit_adUrl.Text =model.ImageUrl;
            SetControlEnable(false);
            simpleButton_modify.Visible = true;            
        }        


        private void simpleButton_add_Click(object sender, EventArgs e)
        {
            if (!CheckPass()) return;
            if (MainForm.PositionService.AdMachineExists(textBox_number.Text))
            {
                XtraMessageBox.Show("该编号已被使用，请使用其他编号！");
                textBox_number.Focus();
                return;
            }
            ADMachineInfo model = new ADMachineInfo();
            model.Number = textBox_number.Text.Trim();
            model.StationName = textEdit_stationName.Text;
            model.Longitude = double.Parse(textEdit_longitude.Text.Trim());
            model.Latitude = double.Parse(textEdit_latitude.Text.Trim());
            model.Linkman = textEdit_linkman.Text;
            model.Telephone = textEdit_telphone.Text;
            model.PhoneNumber = textEdit_phone.Text;
            model.Address = textEdit_address.Text;
            model.InternetType = textEdit_internetType.Text;
            model.XTRemoteId = textEdit_remoteId.Text;
            model.ExitPassword = textEdit_exitPwd.Text.Trim();
            model.ImageUrl = textEdit_adUrl.Text;
            if (MainForm.PositionService.AdMachineAdd(model))
            {
                XtraMessageBox.Show("添加成功！");
                this.Close();
            }
            else
            {
                XtraMessageBox.Show("添加失败！");
            }
        }

        private bool CheckPass()
        {
            if (string.IsNullOrEmpty(textBox_number.Text))
            {
                XtraMessageBox.Show("请输入站点编号！");
                textBox_number.Focus();
                return false;
            }
            if (textEdit_stationName.Text=="")
            {
                XtraMessageBox.Show("请输入站点名称！");
                textEdit_stationName.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_stationName.Text).Length>60)
            {
                XtraMessageBox.Show("站点名称过长！应小于60字节（30个汉字）");
                textEdit_stationName.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_linkman.Text).Length > 30)
            {
                XtraMessageBox.Show("联系人名称过长！应小于30字节（15个汉字）");
                textEdit_linkman.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_address.Text).Length > 100)
            {
                XtraMessageBox.Show("详细地址过长！应小于100字节（50个汉字）");
                textEdit_address.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_telphone.Text).Length > 45)
            {
                XtraMessageBox.Show("电话号码过长！应小于45字节（22个汉字）");
                textEdit_telphone.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_phone.Text).Length > 45)
            {
                XtraMessageBox.Show("手机号码过长！应小于45字节（22个汉字）");
                textEdit_phone.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_internetType.Text).Length > 45)
            {
                XtraMessageBox.Show("入网方式类容过长！应小于45字节（22个汉字）");
                textEdit_internetType.Focus();
                return false;
            }
            if (Encoding.GetEncoding("gbk").GetBytes(textEdit_remoteId.Text).Length >20)
            {
                XtraMessageBox.Show("入网方式类容过长！应小于20字节（10个汉字）");
                textEdit_remoteId.Focus();
                return false;
            }

            return true;
        }

        private void SetControlEnable(bool isenable)
        {
            textBox_number.Enabled = isenable;
            textEdit_adUrl.Enabled = isenable;
            textEdit_address.Enabled = isenable;
            textEdit_exitPwd.Enabled = isenable;
            textEdit_internetType.Enabled = isenable;
            textEdit_latitude.Enabled = isenable;
            textEdit_linkman.Enabled = isenable;
            textEdit_longitude.Enabled = isenable;
            textEdit_phone.Enabled = isenable;
            textEdit_remoteId.Enabled = isenable;
            textEdit_stationName.Enabled = isenable;
            textEdit_telphone.Enabled = isenable;
        }

        private void simpleButton_modify_Click(object sender, EventArgs e)
        {
            if (simpleButton_modify.Text == "修改")
            {
                simpleButton_modify.Text = "保存";
                simpleButton_modify.Location = 
                    simpleButton_add.Location;
                simpleButton_cancel.Visible = true;
                SetControlEnable(true);
                textBox_number.Enabled = false;
            }
            else
            {
                ADMachineInfo model = new ADMachineInfo();
                model.Number = textBox_number.Text.Trim();
                model.StationName = textEdit_stationName.Text;
                model.Longitude = double.Parse(textEdit_longitude.Text.Trim());
                model.Latitude = double.Parse(textEdit_latitude.Text.Trim());
                model.Linkman = textEdit_linkman.Text;
                model.Telephone = textEdit_telphone.Text;
                model.PhoneNumber = textEdit_phone.Text;
                model.Address = textEdit_address.Text;
                model.InternetType = textEdit_internetType.Text;
                model.XTRemoteId = textEdit_remoteId.Text;
                model.ExitPassword = textEdit_exitPwd.Text.Trim();
                model.ImageUrl = textEdit_adUrl.Text;
                if (MainForm.PositionService.AdMachineUpdate(model))
                {
                    XtraMessageBox.Show("修改成功！");
                    simpleButton_modify.Text = "修改";
                    simpleButton_cancel.Visible = false;
                    SetControlEnable(false);
                }
                else
                {
                    XtraMessageBox.Show("修改失败！");
                }
            }
        }

        private void simpleButton_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textEdit_adUrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            AdMachineAdListForm adListForm = 
                new AdMachineAdListForm(textEdit_adUrl.Text);
            adListForm.ShowDialog();
            textEdit_adUrl.Text = adListForm.StrImgUrl;
        }



        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion


    }
}